package com.jch.learn.plugin;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.jch.learn.config.NacosManager;
import com.jch.learn.model.AppConf;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.IPlugin;
import lombok.extern.slf4j.Slf4j;

import java.util.Properties;

@Slf4j
public class NacosPlugin implements IPlugin {

    private AppConf.NacosRegister register;

    private Properties properties;

    public NacosPlugin(AppConf.NacosRegister register) {
        this.register = register;
        properties = PropKit.use("nacos.properties").getProperties();
        PropKit.useless("nacos.properties");
    }

    @Override
    public boolean start() {
        // 初始化 NacosManager
        NacosManager.create(properties);
        // 注册 service instance
        boolean flag = NacosManager.getInstance().register(register.serviceName,register.groupName,register.servicePort);
        log.info("{}:{} - register {}",register.serviceName,register.servicePort,flag ? "success" : "fail");
        return flag;
    }

    @Override
    public boolean stop() {
        boolean flag = NacosManager.getInstance().deregister(register.serviceName,register.groupName,register.servicePort);
        log.info("{}:{} - deregister {}",register.serviceName,register.servicePort,flag ? "success" : "fail");
        return false;
    }
}
